package z.explorer.ui.component.login;

import javax.swing.*;

import z.explorer.Main;
import z.explorer.core.storage.User;
import z.explorer.core.storage.dao.UserDAO;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login extends JDialog {

	public final static int LOGIN = 1;
	public final static int PROFILE = 2;

	private int type = LOGIN;

	JTextField nameField = new JTextField(20);

	JPasswordField passField = new JPasswordField(20);

	JButton loginButton = new JButton("OK");

	JButton cancelButton = new JButton("Cancel");

	public Login(int type) {
		super((JFrame)Main.context.getBean("zExplorer"),"Login",true);
		setLocationRelativeTo(getParent());
		this.type = type;
		initUI();
		if (type == LOGIN) {
			nameField.setEditable(true);
			passField.setEditable(true);
		} else {
			User u = (User) Main.context.getBean("user");
			nameField.setText(u.getName());
			passField.setText(u.getPass());
			nameField.setEditable(false);
			passField.setEditable(false);
		}

		loginButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				User user = (User) Main.context.getBean("user");
				if (Login.this.type == LOGIN) {
					User u = new User();
					u.setName(nameField.getText().trim());
					u.setPass(passField.getText().trim());
					UserDAO uDao = (UserDAO) Main.context.getBean("userDAO");

					if (uDao.login(u) > 0) {
						user.setName(u.getName());
						user.setPass(u.getPass());
					} else {
						user.setName("undefined");
						user.setPass("undefined");
					}
				} else {
					nameField.setText(user.getName());
					passField.setText(user.getPass());
				}
				
				Login.this.dispose();
			}
		});
		
		cancelButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				Login.this.dispose();
				
			}
			
		});

		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		pack();
		setVisible(true);
	}

	public void initUI() {
		Container con = getContentPane();
		con.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();

		JLabel loginLabel = new JLabel("Login In");
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridwidth = 3;
		gbc.gridheight = 1;
		gbc.weightx = 1.0;
		gbc.fill = GridBagConstraints.REMAINDER;
		con.add(loginLabel, gbc);

		JLabel nameLabel = new JLabel("name:");
		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.NONE;
		gbc.weightx = 0.0;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		con.add(nameLabel, gbc);

		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1.0;
		gbc.gridheight = 1;
		gbc.gridwidth = 2;
		con.add(nameField, gbc);

		JLabel passLabel = new JLabel("password:");
		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.fill = GridBagConstraints.NONE;
		gbc.weightx = 0.0;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		con.add(passLabel, gbc);

		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridx = 1;
		gbc.gridy = 2;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1.0;
		gbc.gridheight = 1;
		gbc.gridwidth = 2;
		con.add(passField, gbc);

		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout());
		buttonPanel.add(loginButton);
		buttonPanel.add(cancelButton);

		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx = 0;
		gbc.gridy = 3;
		gbc.gridwidth = 3;
		gbc.gridheight = 1;
		gbc.weightx = 1.0;
		gbc.fill = GridBagConstraints.NONE;
		gbc.anchor = GridBagConstraints.CENTER;
		con.add(buttonPanel, gbc);

	}

	public static void main(String args[]) {
		Login login = new Login(LOGIN);
		login.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		login.pack();
		login.setVisible(true);
	}

}
